package com.example.springboot.service;

import com.example.springboot.entity.ForumPostFavorite;
import com.example.springboot.mapper.ForumPostFavoriteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ForumPostFavoriteService {

    @Autowired
    private ForumPostFavoriteMapper forumPostFavoriteMapper;

    public void favoritePost(ForumPostFavorite favorite) {
        // 检查是否已收藏
        ForumPostFavorite existingFavorite = forumPostFavoriteMapper.selectFavorite(favorite.getPostId(), favorite.getUserId());
        if (existingFavorite != null) {
            // 如果已收藏，可以返回提示信息，而不是抛出异常
            return;  // 或者根据实际需要返回特定错误码或消息
        }
        // 如果未收藏，则执行插入操作
        forumPostFavoriteMapper.insertFavorite(favorite);
    }



    public void unfavoritePost(Integer postId, Integer userId) {
        forumPostFavoriteMapper.deleteFavorite(postId, userId);
    }

    public boolean isPostFavoritedByUser(Integer postId, Integer userId) {
        return forumPostFavoriteMapper.selectFavorite(postId, userId) != null;
    }
    public List<ForumPostFavorite> getFavoritesByUserId(Integer userId) {
        return forumPostFavoriteMapper.findByUserId(userId);
    }
}